<?php
namespace App\Http\Controllers\Admin;

use App\Http\Config\ErrorCode;
use App\Library\TencentHandle;
use App\Models\Admin\AdminUser;
use App\Models\Admin\Articles;
use App\Models\Admin\AssignLog;
use App\Models\Admin\Configs;
use App\Models\Admin\TencentTemp;
use App\Models\Communicationlog;
use App\Models\Customer\Customer;
use App\Models\Customer\CustomerSource;
use App\Models\Member\MemberBase;
use App\Models\NotifyBase;
use App\Models\Saas\Mall;
use App\Models\User\UserBase;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Partners\V20180321\Models\AuditApplyClientRequest;
use TencentCloud\Partners\V20180321\Models\DescribeAgentClientsRequest;
use TencentCloud\Partners\V20180321\PartnersClient;

class CommonController extends Controller
{
    public function __construct()
    {
        /*--- start 跨域测试用 (待删除) ---*/
        header('Access-Control-Allow-Origin: *');                                                                 // 允许任意域名发起的跨域请求
        header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE');
        header('Access-Control-Allow-Headers: X-Requested-With,X_Requested_With');
        header('Access-Control-Allow-Headers:x-requested-with,content-type');
        /*--- end 跨域测试用---*/
    }

    public $result = array("status"=>0,'msg'=>'请求成功','data'=>"");
    //更新售后订单信息
    public function updateAfterSale(){
        $after_arr = DB::table('after_sale as af')
            ->select('af.*','s.rule_type','s.rule_type','s.type','s.after_first_bonus')
            ->leftJoin('salebonusrule as s','af.sbr_id','=','s.id')
            ->where('af.after_status','=',0)
            ->where('af.sbr_id','>',0)
            ->where('s.rule_type',0)
            ->get();
        $after_arr = json_decode(json_encode($after_arr),true);

        foreach ($after_arr as $v) {
            // 计算时间
            $cur_time = Carbon::parse('-' . ($v['after_time'] + 1) . ' months')->toDateTimeString();
            //如果是奖金记录 增加一个月份 跳过
            if ($cur_time >= $v['buy_time']){
                $data_upd['after_status'] = 0;
                $data_upd['after_time'] = $v['after_time'] + 1;
                $data_upd['updated_at'] = Carbon::now()->toDateTimeString();
                if ($data_upd['after_time'] >= $v['buy_length']){
                    $data_upd['after_status'] = 1;
                }
                $this->updateAfterSaleInfo($data_upd, $v['id']);
            }
        }

        $after_arr = DB::table('bonus_sale as bs')
            ->select('bs.*','s.rule_type','s.type','s.first_bonus')
            ->leftJoin('salebonusrule as s','bs.sbr_id','=','s.id')
            ->where('bs.after_status', '=', 0)
            ->where('bs.sbr_id','>',0)
            ->where('s.rule_type',0)
            ->where('s.type',0)
            ->get();
        $after_arr = json_decode(json_encode($after_arr),true);

        foreach ($after_arr as $v) {
            // 计算时间
            $cur_time = Carbon::parse('-' . ($v['after_time'] + 1) . ' months')->toDateTimeString();
            //如果是奖金记录 增加一个月份 跳过
            if ($cur_time >= $v['buy_time']){
                $data_upd['after_status'] = 0;
                $data_upd['after_time'] = $v['after_time'] + 1;
                $data_upd['updated_at'] = Carbon::now()->toDateTimeString();
                if ($data_upd['after_time'] >= $v['buy_length']){
                    $data_upd['after_status'] = 1;
                }
                DB::table('bonus_sale')->where(["id" => $v['id']])->update($data_upd);
            }
        }
        if(DB::table('javascript_status')->where("alias_name", "updateAfterSale")->count()){
            DB::table('javascript_status')->where("alias_name", "updateAfterSale")->update(["exec_time" => Carbon::now()]);
        }else{
            DB::table("javascript_status")->insert(["name" => "更新售后订单数据", "alias_name" => "updateAfterSale", 'exec_time' => Carbon::now()]);
        }
        return response()->json($this->result);
    }

    //提成表修改
    private function updateAfterSaleInfo($fields,$id){
        $res = DB::table('after_sale')->where('id',$id)->update($fields);
        if (!$res){
            Log::info("admin_after_sale_update_error",array("id"=>$id,"update_info"=>$fields));
        }
        return true;
    }

    public function getTitle()
    {
        /*--- start 跨域测试用 (待删除) ---*/
        header('Access-Control-Allow-Origin: *');                                                                 // 允许任意域名发起的跨域请求
        header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE');
        header('Access-Control-Allow-Headers: X-Requested-With,X_Requested_With');
        header('Access-Control-Allow-Headers:x-requested-with,content-type');
        /*--- end 跨域测试用---*/
        $returnData = ErrorCode::$admin_enum["success"];
        $configModel = new Configs();
        $data = $configModel->getFields(["title", "qy_redirect", 'seo_ico', "shortcut", 'member_wechat_qr', "shortcut_name", 'copyright', "shortcut_url", "qywxLogin","seo_title","env",'avatar_status','plugin_open_type as pluginOpenType']);
        $agreementData = DB::table('articles')->whereIn('typeid',[6,7])->where("mall_id", request()->mallId && request()->mallId != "undefined" ? request()->mallId : 1)->select('typeid','title','content')->get();
        $agreementData = json_decode(json_encode($agreementData),true);
        if (!$agreementData){
            $data['agreementTitle'] = '';
            $data['agreementContent'] ='';
            $data['privacyTitle'] = '';
            $data['privacyContent'] = '';
        }else{
            foreach ($agreementData as $v){
                if ($v['typeid'] == 6){
                    $data['agreementTitle'] = $v['title'];
                    $data['agreementContent'] =$v['content'];
                }else{
                    $data['privacyTitle'] = $v['title'];
                    $data['privacyContent'] =$v['content'];
                }
            }
        }
        $returnData['data'] = $data;
        return response()->json($returnData);
    }

    //站点信息
    public function other()
    {
        $arr[] = array('con_name' => '软件版本', 'con_value' => 'V3.4');
        $arr[] = array('con_name' => '联系我们', 'con_value' => '手机/微信：13670093216 QQ：1018608475');
        $arr[] = array('con_name' => '版权所有', 'con_value' => '深圳市网商天下科技开发有限公司');
        $arr[] = array('con_name' => '总策划兼项目经理', 'con_value' => '车贯 （Michael）');
        $arr[] = array('con_name' => '研发团队', 'con_value' => '陈元  许堉颖  陈镇松  李迪  杜新科  刘丹');
        $arr[] = array('con_name' => '设计团队', 'con_value' => '龙芳 林鸿耿');
        $arr[] = array('con_name' => '特别支持', 'con_value' => '刘远航  袁程远');
        $returnData['data'] = $arr;
        return response()->json($returnData);
    }

    // 自动审核腾讯云客户
    public function verifyQCloudCustomerList(){
        require_once  base_path().'/vendor/tencentcloud-sdk-php/TCloudAutoLoader.php';
        $mallList = Mall::getFields("id", [], false);
        foreach ($mallList as $mall) {
            $con = Configs::query()->where("mall_id", $mall["id"])->select("tencent_secretid", "tencent_secrekey")->first();
            $cred = new Credential($con->tencent_secretid, $con->tencent_secrekey);
            $httpProfile = new HttpProfile();
            $httpProfile->setEndpoint("partners.tencentcloudapi.com");

            $clientProfile = new ClientProfile();
            $clientProfile->setHttpProfile($httpProfile);
            $client = new PartnersClient($cred, "", $clientProfile);
            $req = new DescribeAgentClientsRequest();
            $req->ClientFlag = 'a';
            $resp = $client->DescribeAgentClients($req);
            $res = $resp->toJsonString();
            $res = json_decode($res, true);
            if ($res['TotalCount'] > 0) {
                $data = $res['AgentClientSet'];
                if (!is_array($data) || count($data) < 1) {
                    return response()->json(['status' => 99, 'msg' => '接口报错，返回失败']);
                }
                $verify_res = $this->_verifyQCloudCustomer($data, $mall["id"]);
            }
        }
        if(DB::table('javascript_status')->where("alias_name", "verifyQCloudCustomerList")->count()){
            DB::table('javascript_status')->where("alias_name", "verifyQCloudCustomerList")->update(["exec_time" => Carbon::now()]);
        }else{
            DB::table("javascript_status")->insert(["name" => "审核腾讯云客户", "alias_name" => "verifyQCloudCustomerList", 'exec_time' => Carbon::now()]);
        }
        $data['code'] = 0;
        $data['msg'] = '请求成功';
        return response()->json($data);
    }

    public function _verifyQCloudCustomer($data, $mallId){
        require_once  base_path().'/vendor/tencentcloud-sdk-php/TCloudAutoLoader.php';
        $con = Configs::query()->where("mall_id", $mallId)->select("tencent_secretid", "tencent_secrekey")->first();
        $cred = new Credential($con->tencent_secretid, $con->tencent_secrekey);
        $httpProfile = new HttpProfile();
        $httpProfile->setEndpoint("partners.tencentcloudapi.com");

        $clientProfile = new ClientProfile();
        $clientProfile->setHttpProfile($httpProfile);
        $client = new PartnersClient($cred, "", $clientProfile);

        foreach ($data as $v) {
            try {
                $req = new AuditApplyClientRequest();
                $req->ClientUin = $v['ClientUin'];
                $req->AuditResult = 'accept';   //同意
                $req->Note = '1.客户情况：个人  2.预计每月订单与消耗：1K-3K  3.为客户提供何种服务：售前售后售中技术咨询服务'; //申请理由      B类客户审核时必须填写申请理由 有腾讯云审核

                $resp = $client->AuditApplyClient($req);
                $res = $resp->toJsonString();
                $res = json_decode($res, true);
                if (isset($res['Response']['Error'])) {
                    Log::info($v['ClientUin'] . ' verify fail. ', array('result' => $res));
                } else {
                    Log::info($v['ClientUin'] . ' verify success. ', array('result' => $res));
                }
            }catch (\Exception $e){
                continue;
            }
        }
    }

    public function uploadzipfile()
    {
        $data['code'] = 0;
        $data['msg'] = '该用户可以上传文件';
        return response()->json($data);
    }

    //修改活动状态定时任务接口
    public function updateActivityStatus()
    {
        $activity_arr = DB::table('activity')
            ->select('id','activity_status','start_time','end_time','stop_time')
            ->whereIn('activity_status',[1,2,3])
            ->get();
        $activity_arr = json_decode(json_encode($activity_arr), true);
        if (!$activity_arr){
            return response()->json($this->result);
        }
        $now_time = Carbon::now()->toDateTimeString();
        foreach ($activity_arr as $v) {
            $where['activity_status'] = '';
            switch ($v['activity_status']){
                case 1;                  //报名中
                    if ($v['stop_time'] < $now_time){
                        $where['activity_status'] = 2;
                    }
                    break;
                case 2;                 //报名截止
                    if ($v['start_time'] < $now_time){
                        $where['activity_status'] = 3;
                    }
                    break;
                case 3;                 //活动进行中
                    if ($v['end_time'] < $now_time){
                        $where['activity_status'] = 4;
                    }
                    break;
                default;
                    $where['activity_status'] = '';
                   break;
            }
            if ($where['activity_status'] != ''){
                DB::table('activity')->where('id', $v['id'])->update($where);
            }
        }
        if(DB::table('javascript_status')->where("alias_name", "updateActivityStatus")->count()){
            DB::table('javascript_status')->where("alias_name", "updateActivityStatus")->update(["exec_time" => Carbon::now()]);
        }else{
            DB::table("javascript_status")->insert(["name" => "活动状态修改", "alias_name" => "updateActivityStatus", 'exec_time' => Carbon::now()]);
        }
        return response()->json($this->result);
    }

    //丢单/下次联系小于当前时间，客户重新分配
    public function customerUpdateInfo(){
        $res =  DB::table('customer')
            ->where(function ($query) {
                $query->where('contact_next_time','<',Carbon::createFromTimestamp(mktime(0,0,0,date('m'),date('d')-7,date('Y')))->toDateTimeString())
                    ->Orwhere('progress','丢单');
            })
            ->where('contact_next_time','!=','0000-00-00 00:00:00')
            ->where('cust_state','!=',1)
            ->where('recommend','!=',1)
            ->get();
        $res = json_decode(json_encode($res),true);
        $where['recommend'] = 1;
        foreach ($res as $v){
            $assign_log_data[] = array(
                'member_id' => $v['id'],
                'assign_name' => AdminUser::query()->where("id", $v["recommend"])->value("name"),
                'assign_admin' => "root",
                'assign_uid' => $v['recommend'],
                'assign_touid' => 1,
                'operation_uid' => 1
            );
            DB::table('customer')->where('id',$v['id'])->update($where);
        }
        $assignLogModel = new AssignLog();
        $assignLogModel->assignLogInsert($assign_log_data,2);
        $data['code'] = 0;
        $data['msg'] = '请求成功';
        if(DB::table('javascript_status')->where("alias_name", "customerUpdateInfo")->count()){
            DB::table('javascript_status')->where("alias_name", "customerUpdateInfo")->update(["exec_time" => Carbon::now()]);
        }else{
            DB::table("javascript_status")->insert(["name" => "逾期7天-客户重新分配", "alias_name" => "customerUpdateInfo", 'exec_time' => Carbon::now()]);
        }
        return response()->json($data);
    }

    //删除过期form_id
    public function timingUpdateFormId(){
        $exceedTime = date("Y-m-d 00:00:00",strtotime("-5 day"));
        $res =  DB::table('form_id')
            ->where('create_time','<',$exceedTime)
            ->select('id')
            ->get();

        $res = json_decode(json_encode($res),true);
        if (!$res){
            $data['code'] = 0;
            $data['msg'] = '请求成功';
            return response()->json($data);
        }
        $ids = [];
        foreach ($res as $v){
            $ids[] = $v['id'];
        }
        DB::table('form_id')->whereIn('id',$ids)->delete();
        if(DB::table('javascript_status')->where("alias_name", "timingUpdateFormId")->count()){
            DB::table('javascript_status')->where("alias_name", "timingUpdateFormId")->update(["exec_time" => Carbon::now()]);
        }else{
            DB::table("javascript_status")->insert(["name" => "删除过期form_id", "alias_name" => "timingUpdateFormId", 'exec_time' => Carbon::now()]);
        }
        $data['code'] = 0;
        $data['msg'] = '请求成功';
        return response()->json($data);
    }

    //下载文件
    public function download(Request $request){
        $con = Configs::query()->where("mall_id", request()->mallId && request()->mallId != "undefined" ? request()->mallId : 1)->first();
        if ($con->env == 'CLOUD'){
            $url = $request->post('url');
            $info = pathinfo($url);
            $data['code'] = 3;
            $data['name'] = $info['basename'];
            $data['data'] = $url;
            return $data;
        }else{
            $file_name = $request->input('url');
            if(strpos($file_name, "http") === false) {
                $file_name = request()->getSchemeAndHttpHost() . '/' . $file_name;
            }
            ob_start();
            $context = stream_context_create(
                array('http' => array(
                    'follow_location' => false // don't follow redirects
                ))
            );
            readfile($file_name, false, $context);
            $img = ob_get_contents();
            ob_end_clean();
            $temp_file = tempnam(sys_get_temp_dir(), "php");
            file_put_contents($temp_file, $img);
            $size = filesize ($temp_file);
            $mime = 'application/force-download';
            header('Pragma: public'); // required
            header('Expires: 0'); // no cache
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Cache-Control: private',false);
            header('Content-Type: '.$mime);
            header("Accept-ranges:bytes");
            header("Content-Length:".$size);
            header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
            header('Content-Transfer-Encoding: binary');
            header('Connection: close');
            readfile($temp_file); // push it out
            exit();
        }
    }

    //根据设置提醒时间通知运维和客户经理
    public function updateRemindTime(){
        $res = DB::table('achievement_extend as ae')->leftJoin('member as m' ,'m.id','=','ae.member_id')->whereNotNull('ae.remind_time')->select(["ae.*", "m.mall_id"])->get();
        $res = json_decode(json_encode($res),true);
        if (!$res){
            $data['code'] = 0;
            $data['msg'] = '请求成功';
            return response()->json($data);
        }
        $notifyModel = new NotifyBase();
        $adminBaseModel = new UserBase();
        $memberBaseModel = new MemberBase();
        //日期计算
        foreach ($res as $v){
            if(empty($con[$v["mall_id"]])) {
                $con[$v["mall_id"]] = Configs::query()->where("mall_id", $v["mall_id"])->select(["qywxLogin"])->first();
            }
            $set_time = strtotime($v['remind_time']);
            $now_time = strtotime(date('Y-m-d 00:00:00',time()));
            $surplus_time = ($set_time - $now_time)/86400;
            if ($surplus_time == 0){  //到期 发送通知
                if ($con[$v["mall_id"]]->qywxLogin == 1){
                    \request()->mallData = ["id" => $v["mall_id"]];
                    $member_info = $memberBaseModel->getMemberByID($v['member_id']);
                    if (isset($v['manager_id'])){
                        $manager_info = $adminBaseModel->getAdminByID($v['manager_id']);
                        if ($manager_info){
                            $params = array(
                                'type' => 'ach_success',
                                'content' => '您设置的'.$member_info['name'].'客户的提醒时间为今天，请您及时处理，并更新提醒时间',
                                'receive_wechatid' => $manager_info['email'],
                            );
                            $notifyModel->sendQYWechat($params);
                        }
                    }
                    if (isset($v['maintain_id'])){
                        $maintain_info = $adminBaseModel->getAdminByID($v['maintain_id']);
                        if ($maintain_info){
                            $params = array(
                                'type' => 'ach_success',
                                'content' => '您设置的'.$member_info['name'].'客户的提醒时间为今天，请您及时处理，并更新提醒时间',
                                'receive_wechatid' => $maintain_info['email'],
                            );
                            $notifyModel->sendQYWechat($params);
                        }
                    }
                }
            }
            $where['expire'] = $surplus_time;
            $update_res = DB::table('achievement_extend')->where('id',$v['id'])->update($where);
            if (!$update_res){
                Log::info('update achievement_extend fail id is' .$v['id']);
                $data['code'] = 0;
                $data['msg'] = '请求成功';
                return response()->json($data);
            }
        }
        if(DB::table('javascript_status')->where("alias_name", "updateRemindTime")->count()){
            DB::table('javascript_status')->where("alias_name", "updateRemindTime")->update(["exec_time" => Carbon::now()]);
        }else{
            DB::table("javascript_status")->insert(["name" => "根据设置提醒时间通知运维和客户经理", "alias_name" => "updateRemindTime", 'exec_time' => Carbon::now()]);
        }
        $data['code'] = 0;
        $data['msg'] = '请求成功';
        return response()->json($data);
    }

    //记录平台运营数据
    public function recordOperateData(){
        //月份
        $month = date('Y-m');
        $data['month'] = $month;
        $start_time = date('Y-m-01 00:00:00');
        $end_time = date('Y-m-t 23:59:59');
        $mallList = Mall::getFields("id", [], false);
        foreach ($mallList as $mall) {
            //计算总客户量
            $total_customer = DB::table('customer')->whereBetween('created_at', [$start_time, $end_time])->where("mall_id", $mall["id"])->select('source')->get();
            $total_customer = json_decode(json_encode($total_customer), true);
            $data['customer_number'] = count($total_customer);
            //总激活量
            $total_member = DB::table('member as m')->leftJoin('member_extend as me', 'm.id', '=', 'me.member_id')->whereBetween('create_time', [$start_time, $end_time])->where("m.mall_id", $mall["id"])->select('source')->get();
            $total_member = json_decode(json_encode($total_member), true);
            $data['member_number'] = count($total_member);
            //本月业绩
            $ach_res = DB::table('achievement')->select(DB::raw('sum(goods_money) AS total_money'))
                ->whereBetween('buy_time', [$start_time, $end_time])
                ->where('status', 1)
                ->where('ach_state', 0)
                ->where("mall_id", $mall["id"])
                ->get();
            $ach_res = json_decode(json_encode($ach_res), true);
            $money = 0;
            foreach ($ach_res as $v) {
                $money = $money + $v['total_money'];
            }
            $data['bonus_number'] = $money;
            //计算来源
            $sou_res = DB::table('member_source')->select('source_name')->where("mall_id", $mall["id"])->get();
            $sou_res = json_decode(json_encode($sou_res), true);
            $data['source'] = json_encode([]);
            if(!empty($sou_res)) {
                foreach ($sou_res as &$v) {
                    $v['month_customer'] = 0;
                    $v['month_member'] = 0;
                    foreach ($total_customer as $c_v) {
                        if ($c_v['source'] == $v['source_name']) {
                            $v['month_customer'] += 1;
                        }
                    }
                    foreach ($total_member as $m_v) {
                        if ($m_v['source'] == $v['source_name']) {
                            $v['month_member'] += 1;
                        }
                    }
                }
                $data['source'] = json_encode($sou_res);
            }
            //成单
            if ($data['member_number'] == 0 && $data['customer_number'] == 0) {
                $data['order_form'] = 0;
            } elseif ($data['member_number'] != 0 && $data['customer_number'] == 0) {
                $data['order_form'] = $total_member;
            } else {
                $data['order_form'] = (int)(sprintf('%.2f', $data['member_number'] / $data['customer_number']) * 100);
            }
            //丢单
            $data['lose_order'] = 100 - $data['order_form'];
            $data['id'] = 0;
            $data['type'] = 1;
            $res = DB::table('operate_log')->where('month', $month)->where('type', 1)->where("mall_id", $mall["id"])->where('id', 0)->first();
            if (!$res) {
                $data["mall_id"] = $mall["id"];
                DB::table('operate_log')->insert($data);
            } else {
                DB::table('operate_log')->where('log_id', $res->log_id)->update($data);
            }
        }
        if(DB::table('javascript_status')->where("alias_name", "recordOperateData")->count()){
            DB::table('javascript_status')->where("alias_name", "recordOperateData")->update(["exec_time" => Carbon::now()]);
        }else{
            DB::table("javascript_status")->insert(["name" => "记录平台运营数据", "alias_name" => "recordOperateData", 'exec_time' => Carbon::now()]);
        }
        return response()->json($this->result);
    }

    //记录平台部门运营数据
    public function CompanyOperateData(){
        $mallList = Mall::getFields("id", [], false);
        foreach ($mallList as $mall) {
            $company_res = DB::table('company')->where("mall_id", $mall["id"])->get();
            $company_res = json_decode(json_encode($company_res), true);
            $userModel = new UserBase();
            $month = date('Y-m');
            $start_time = date('Y-m-01 00:00:00');
            $end_time = date('Y-m-t 23:59:59');
            request()->mallData = ["id" => $mall["id"]];
            foreach ($company_res as $v) {
                $ids = $userModel->getCompanyIdList($v['id']);
                $data['month'] = $month;
                $data['id'] = $v['id'];
                $data['type'] = 2;
                //计算本月客户量
                $total_customer = DB::table('customer')->whereBetween('created_at', [$start_time, $end_time])->select('source')->whereIn('recommend', $ids)->where("mall_id", $mall["id"])->get();
                $total_customer = json_decode(json_encode($total_customer), true);
                $data['customer_number'] = count($total_customer);
                //计算激活量
                $total_member = DB::table('member as m')->leftJoin('member_extend as me', 'm.id', '=', 'me.member_id')->whereBetween('m.create_time', [$start_time, $end_time])->select('source')->whereIn('me.recommend', $ids)->where("m.mall_id", $mall["id"])->get();
                $total_member = json_decode(json_encode($total_member), true);
                $data['member_number'] = count($total_member);
                //本月业绩
                $ach_res = DB::table('achievement')->select(DB::raw('sum(goods_money) AS total_money'))
                    ->whereBetween('buy_time', [$start_time, $end_time])
                    ->whereIn('admin_users_id', $ids)
                    ->where('status', 1)
                    ->where('ach_state', 0)
                    ->where("mall_id", $mall["id"])
                    ->get();
                $ach_res = json_decode(json_encode($ach_res), true);
                $money = 0;
                foreach ($ach_res as $a_v) {
                    $money = $money + $a_v['total_money'];
                }
                $data['bonus_number'] = $money;
                //计算来源
                $sou_res = DB::table('member_source')->select('source_name')->where("mall_id", $mall["id"])->get();
                if (!$sou_res) {
                    $data['source'] = json_encode([]);
                } else {
                    $sou_res = json_decode(json_encode($sou_res), true);
                    foreach ($sou_res as &$s_v) {
                        $s_v['month_customer'] = 0;
                        $s_v['month_member'] = 0;
                        foreach ($total_customer as $c_v) {
                            if ($c_v['source'] == $s_v['source_name']) {
                                $s_v['month_customer'] += 1;
                            }
                        }
                        foreach ($total_member as $m_v) {
                            if ($m_v['source'] == $s_v['source_name']) {
                                $s_v['month_member'] += 1;
                            }
                        }
                    }
                    $data['source'] = json_encode($sou_res);
                }
                //成单
                if ($data['member_number'] == 0 && $data['customer_number'] == 0) {
                    $data['order_form'] = 0;
                } elseif ($data['member_number'] != 0 && $data['customer_number'] == 0) {
                    $data['order_form'] = $total_member;
                } else {
                    $data['order_form'] = (int)(sprintf('%.2f', $data['member_number'] / $data['customer_number']) * 100);
                }
                //丢单
                $data['lose_order'] = 100 - $data['order_form'];
                $res = DB::table('operate_log')->where('month', $month)->where('type', 2)->where("mall_id", $mall["id"])->where('id', $v['id'])->first();
                if (!$res) {
                    $data["mall_id"] = $mall["id"];
                    DB::table('operate_log')->insert($data);
                } else {
                    DB::table('operate_log')->where('log_id', $res->log_id)->update($data);
                }
            }
        }
        if(DB::table('javascript_status')->where("alias_name", "CompanyOperateData")->count()){
            DB::table('javascript_status')->where("alias_name", "CompanyOperateData")->update(["exec_time" => Carbon::now()]);
        }else{
            DB::table("javascript_status")->insert(["name" => "记录平台部门运营数据", "alias_name" => "CompanyOperateData", 'exec_time' => Carbon::now()]);
        }
        return response()->json($this->result);
    }

    //记录平台个人运营数据
    public function personalOperateData(){
        $mallList = Mall::getFields("id", [], false);
        foreach ($mallList as $mall) {
            $users_res = DB::table('admin_users as au')
                ->select('au.id')
                ->leftJoin('admin_users_extend as aue', 'au.id', '=', 'aue.admin_id')
                ->where('au.status', 0)
                ->where('aue.job_status', 1)
                ->where("au.mall_id", $mall["id"])
                ->get();
            $users_res = json_decode(json_encode($users_res), true);
            $month = date('Y-m');
            $start_time = date('Y-m-01 00:00:00');
            $end_time = date('Y-m-t 23:59:59');
            foreach ($users_res as $v) {
                $data['month'] = $month;
                $data['id'] = $v['id'];
                $data['type'] = 3;
                //计算本月客户量
                $total_customer = DB::table('customer')->whereBetween('created_at', [$start_time, $end_time])->select('source')->where('recommend', $v['id'])->where("mall_id", $mall["id"])->get();
                $total_customer = json_decode(json_encode($total_customer), true);
                $data['customer_number'] = count($total_customer);
                //计算激活量 正式客户
                $total_member = DB::table('member as m')
                    ->select('me.source')
                    ->leftJoin('member_extend as me', 'm.id', '=', 'me.member_id')
                    ->where('me.recommend', $v['id'])
                    ->whereBetween('m.create_time', [$start_time, $end_time])
                    ->where("m.mall_id", $mall["id"])
                    ->get();
                $total_member = json_decode(json_encode($total_member), true);
                $data['member_number'] = count($total_member);
                //本月业绩
                $ach_res = DB::table('achievement')->select(DB::raw('sum(goods_money) AS total_money'))
                    ->whereBetween('buy_time', [$start_time, $end_time])
                    ->where('admin_users_id', $v['id'])
                    ->where('status', 1)
                    ->where('ach_state', 0)
                    ->where("mall_id", $mall["id"])
                    ->get();
                $ach_res = json_decode(json_encode($ach_res), true);
                $money = 0;
                foreach ($ach_res as $a_v) {
                    $money = $money + $a_v['total_money'];
                }
                $data['bonus_number'] = $money;
                //计算来源
                $sou_res = DB::table('member_source')->select('source_name')->where("mall_id", $mall["id"])->get();
                if (!$sou_res) {
                    $data['source'] = json_encode([]);
                } else {
                    $sou_res = json_decode(json_encode($sou_res), true);
                    foreach ($sou_res as &$s_v) {
                        $s_v['month_customer'] = 0;
                        $s_v['month_member'] = 0;
                        foreach ($total_customer as $c_v) {
                            if ($c_v['source'] == $s_v['source_name']) {
                                $s_v['month_customer'] += 1;
                            }
                        }
                        foreach ($total_member as $m_v) {
                            if ($m_v['source'] == $s_v['source_name']) {
                                $s_v['month_member'] += 1;
                            }
                        }
                    }
                    $data['source'] = json_encode($sou_res);
                }
                //成单
                if ($data['member_number'] == 0 && $data['customer_number'] == 0) {
                    $data['order_form'] = 0;
                } elseif ($data['member_number'] != 0 && $data['customer_number'] == 0) {
                    $data['order_form'] = $total_member;
                } else {
                    $data['order_form'] = (int)(sprintf('%.2f', $data['member_number'] / $data['customer_number']) * 100);
                }
                //丢单
                $data['lose_order'] = 100 - $data['order_form'];
                $res = DB::table('operate_log')->where('month', $month)->where('type', 3)->where("mall_id", $mall["id"])->where('id', $v['id'])->first();
                if (!$res) {
                    $data["mall_id"] = $mall["id"];
                    DB::table('operate_log')->insert($data);
                } else {
                    DB::table('operate_log')->where('log_id', $res->log_id)->update($data);
                }
            }
        }
        if(DB::table('javascript_status')->where("alias_name", "personalOperateData")->count()){
            DB::table('javascript_status')->where("alias_name", "personalOperateData")->update(["exec_time" => Carbon::now()]);
        }else{
            DB::table("javascript_status")->insert(["name" => "记录平台个人运营数据", "alias_name" => "personalOperateData", 'exec_time' => Carbon::now()]);
        }
        return response()->json($this->result);
    }

    /* 添加客户 */
    public function create(Request $request){
        $customer['qq'] = $request->post('qq','');
        $customer['remarks'] = $request->post('remarks','');
        $customer['name'] = $request->post('name','');
        $customer['source'] = $request->post('source','腾讯云市场');
        if (!$customer['qq'] || !$customer['remarks'] || !$customer['name']){
            $data['code'] = ErrorCode::$admin_enum['fail'];
            $data['msg'] = '添加失败';
            return response()->json($data);
        }
        $mallId = $request->mallId && $request->mallId != "undefined" ? request()->mallId : 1;
        $res = DB::table('customer')->where('qq', $customer['qq'])->where("mall_id", $mallId)->orderBy("id", "desc")->first();
        if ($res) {
            $id = $res->id;
            if (!$model = CustomerSource::query()->where([
                "mall_id" => $request->mallData["id"], "customer_id" => $id, "content" => $customer["source"]
            ])->first()) {
                CustomerSource::query()->insert([
                    "customer_id" => $id, "mall_id" => $request->mallData["id"],
                    "content" => $customer["source"], "remark" => "产品使用",
                    "create_time" => Carbon::now()->toDateTimeString()
                ]);
            } else {
                CustomerSource::query()->where(["id" => $model->id])->update([
                    "create_time" => Carbon::now()->toDateTimeString()
                ]);
            }
            Customer::query()->where("id", $res["id"])->update(["source_time" => Carbon::now()->toDateTimeString()]);
        } else {
            $customer['status'] = 1;
            $customer['realname'] = $customer['name'];
            $customer['created_at'] = Carbon::now()->toDateTimeString();
            $customer["mall_id"] = $mallId;
            $customer["source_time"] = Carbon::now()->toDateTimeString();
            $id = DB::table('customer')->insertGetId($customer);
            $assign['member_id'] = $id;
            $assign['assign_name'] = 'root';
            $assign['assign_admin'] = 'root';
            $assign['assign_admin'] = 'root';
            $assign['created_at'] = Carbon::now()->toDateTimeString();
            $assign['assign_uid'] = 1;
            $assign['assign_touid'] = 1;
            $assign['operation_uid'] = 1;
            $assign["mall_id"] = $mallId;
            $assign_res = DB::table('assign_log')->insert($assign);
            if (!$id || !$assign_res) {
                $data['code'] = ErrorCode::$admin_enum['fail'];
                $data['msg'] = '添加失败';
                return response()->json($data);
            }
        }
        $communicateModel = new Communicationlog();
        $communicateModel->communicationLogInsert([
            'member_id' => $id,
            'admin_user_id' => 1,
            'comm_time' => Carbon::now()->toDateTimeString(),
            'contentlog' => trim($customer['remarks'])
        ]);
        $data['code'] = 0;
        $data['msg'] = '添加成功';
        return response()->json($data);
    }

    //云函数创建数据库
    public function createDatabase(){
        global $scf_data;
        $host = $scf_data['system']['database']['hostname'];
        $port = $scf_data['system']['database']['hostPort'];
        $database = $scf_data['system']['database']['database'];
        $username = $scf_data['system']['database']['username'];
        $password = $scf_data['system']['database']['password'];
        $target_folder = dirname(public_path());
        $target_file = $target_folder . '/vue.sql';
        if (file_exists($target_file)){
            $_sql = file_get_contents($target_file);
            $_arr = explode(';', $_sql);
            $conn = new \mysqli($host.":".$port, $username, $password);
            // 检测连接
            if ($conn->connect_error) {
                $data['code'] = 1;
                $data['msg'] = '数据库配置无效';
                $data['data'] = '';
                return response()->json($data);
            }
            if (!mysqli_select_db($conn,$database)){
                $sql = "CREATE DATABASE ".$database;
                if ($conn->query($sql) != TRUE) {
                    $data['code'] = 1;
                    $data['msg'] = '创建数据库失败';
                    $data['data'] = '';
                    return response()->json($data);
                }
            }
            //执行sql语句
            foreach ($_arr as $k=>$_value) {
                $conn->query($_value . ';');
            }
            $conn->close();
            $data['code'] = 1;
            $data['msg'] = '初始化成功';
            return response()->json($data);
        }
    }

    public function test(Request $request){ // 云开发的定时任务处理
        $this->updateArticle();
        $this->updateAfterSale();
//        $this->verifyQCloudCustomerList();
        $this->updateActivityStatus();
        $this->customerUpdateInfo();
//        $this->timingUpdateFormId();
        $this->updateRemindTime();
        $this->recordOperateData();
        $this->CompanyOperateData();
        $this->personalOperateData();

        echo "========";
    }

    // 更新文章
    public function updateArticle(){
        $articleData = Articles::query()->select("id","day_refresh","auth_refresh",'created_at')->get();
        if($articleData){
            $articleData = $articleData->toArray();
            foreach ($articleData as $item){
                if($item["auth_refresh"] == '1'){
                    if($item["day_refresh"] == '0'){
                        Articles::query()->where("id", $item["id"])->update(['created_at' => date("Y-m-d H:i:s", time()+28800)]);
                    }elseif($item["day_refresh"] > 0 && ($item["day_refresh"] * 86400 + strtotime($item["created_at"])) <= time()){
                        Articles::query()->where("id", $item["id"])->update(['created_at' => date("Y-m-d H:i:s", time()+28800)]);
                    }
                }
            }
        }
        if(DB::table('javascript_status')->where("alias_name", "updateArticle")->count()){
            DB::table('javascript_status')->where("alias_name", "updateArticle")->update(["exec_time" => Carbon::now()]);
        }else{
            DB::table("javascript_status")->insert(["name" => "更新文章的发布时间", "alias_name" => "updateArticle", 'exec_time' => Carbon::now()]);
        }
    }

    public function javascriptHandleTencentData(){
        $database = TencentTemp::query()->where("status", "=", 1)->get();
        $request = request();
        foreach ($database as $item){
            $request->mallData = ["id" => $item->mall_id];
            $item->status = 2;
            $item->save();
            TencentHandle::getInstance()->handleOrderData([
                "startTime" => $item->start_time, "endTime" => $item->end_time,
                "object" => $item
            ]);
        }
    }
}